package com.android.lovely.ui.widget

import android.content.Context
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import androidx.core.view.updateLayoutParams
import com.android.lovely.BuildConfig
import com.android.lovely.R
import com.android.lovely.databinding.LayoutSplashLoginBinding
import com.android.lovely.ext.startWebView
import com.android.lovely.framework.UIController.showToast
import com.android.lovely.ui.activity.LoginActivity
import com.android.lovely.ui.viewmodel.LoginViewModel
import com.kotlin.extensions.context.ActivityExt.navBarHeight
import com.kotlin.extensions.context.ContextExt.startPage
import com.kotlin.extensions.dimen.Dimension.dp
import com.kotlin.extensions.inflater.LayoutInflaterExt.layoutInflater
import com.kotlin.extensions.view.ViewExt.clickable

/**
 *
 * @author why
 * @date 2025/3/8 01:20
 */
class SplashLoginWidget @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    private val mBinding by lazy {
        LayoutSplashLoginBinding.inflate(layoutInflater, this, false)
    }

    init {
        addView(mBinding.root)
    }

    fun update(viewModel: LoginViewModel) {
        mBinding.tvPolicy.updateLayoutParams<MarginLayoutParams> {
            bottomMargin = navBarHeight + 40.dp
        }
        mBinding.tvPolicy.movementMethod = LinkMovementMethod.getInstance()
        initAgreement(mBinding.tvPolicy)
        mBinding.ivCheck.clickable {
            it.isSelected = it.isSelected.not()
        }
        mBinding.tvEmail.clickable {
            context.startPage<LoginActivity>()
        }
        mBinding.tvGuest.clickable {
            if (mBinding.ivCheck.isSelected.not()) {
                showToast(context.getString(R.string.agree_terms))
                return@clickable
            }
            mBinding.tvGuest.isEnabled = false
            viewModel.guestLogin {
                mBinding.tvGuest.isEnabled = true
            }
        }
    }

    private fun initAgreement(cbAgreement: TextView) {
        val agreement = context.getString(R.string.login_provice)
        try {
            cbAgreement.movementMethod = LinkMovementMethod.getInstance()
            val spaBuilder = SpannableStringBuilder(agreement)
            val privacySpan = context.getString(R.string.login_privacy_agreement)
            val serviceSpan = context.getString(R.string.login_user_agreement)
            spaBuilder.setSpan(
                object : ClickableSpan() {
                    override fun onClick(widget: View) {
                        (widget as TextView).highlightColor =
                            resources.getColor(R.color.transparent)
                        startWebView(
                            context.getString(R.string.login_privacy_agreement),
                            BuildConfig.TERM
                        )
                    }

                    override fun updateDrawState(ds: TextPaint) {
                        super.updateDrawState(ds)
                        ds.color = resources.getColor(R.color.color_main)
                        ds.isUnderlineText = false
                        ds.clearShadowLayer()
                    }
                },
                spaBuilder.indexOf(privacySpan),
                spaBuilder.indexOf(privacySpan) + privacySpan.length,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
            )
            spaBuilder.setSpan(
                object : ClickableSpan() {
                    override fun onClick(widget: View) {
                        (widget as TextView).highlightColor =
                            resources.getColor(R.color.transparent)
                        startWebView(
                            context.getString(R.string.login_user_agreement),
                            BuildConfig.PRIVACY
                        )
                    }

                    override fun updateDrawState(ds: TextPaint) {
                        super.updateDrawState(ds)
                        ds.color = resources.getColor(R.color.color_main)
                        ds.isUnderlineText = false
                        ds.clearShadowLayer()
                    }
                },
                spaBuilder.indexOf(serviceSpan),
                spaBuilder.indexOf(serviceSpan) + serviceSpan.length,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
            )

            cbAgreement.setText(spaBuilder, TextView.BufferType.SPANNABLE)
        } catch (e: Exception) {
            cbAgreement.text = agreement
        }
    }
}